package jam.network.message;

import jam.model.*;
import jam.network.*;
import java.util.*;

//
// =============================================================================
//

public class ClientActionPassword extends ClientCommand {
    protected static final String KEYWORD = "CA_PASSWORD";
    
    private String oldPassword;
    private String newPassword;
    
    //
    // =========================================================================
    //
    
    /**
     */
    @Override
    public void process(ClientController controller) {
        ClientModel client = controller.getModel();
        User user = client.getUser();
        
        // Check if the old password matches
        if (user.passwordMatches(this.oldPassword) == false) {
            controller.send(new ServerResponse(this, false));
            return;
        }
        
        // Set the password.
        user.setPassword(this.newPassword);
        
        // Respond to the first client.
        controller.send(new ServerResponse(this, true));
    }
    
    //
    // =========================================================================
    //
    
    @Override
    protected String getKeyword() { return ClientActionPassword.KEYWORD; }
    
    //
    // =========================================================================
    //
    
    /**
     * Constructor. Processes the data string.
     *
     * @param transactionId The identification number for this particular message.
     * @param args A scanner with the arguments
     */
    protected ClientActionPassword (int transactionId, Scanner args) {
        super(transactionId);
        
        // Read in the message arguments.
        this.oldPassword = args.next();
        this.newPassword = args.next();
    }
}
